#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "subpages/checkinmanagementpage.h"
#include "subpages/usermanagemenpage.h"
#include "subpages/checkinpage.h"
#include "subpages/jobmanagementpage.h"
#include "subpages/dormitorymanagementpage.h"
#include "subpages/powermanagementpage.h"
#include "usermanager.h"
#include <QTextEdit>
#include <QMdiSubWindow>
#include <QDebug>
#include "loginwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , userManagementMenu(nullptr)
    , checkinManagementMenu(nullptr)
    , jobManagementMenu(nullptr)
    , powerManagementMenu(nullptr)
    , dormitoryManagementMenu(nullptr)
{
    ts = this;

    ui->setupUi(this);
    setupMenus();

    if (UserManager::getInstance().getRole() == UserManager::Unlogin) {
        onCheckinActionCliked();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupMenus()
{
    QMenuBar *menuBar = ui->menubar;

    menuBar->addMenu(tr("登录"))->addAction(tr("用户登录"), this, &MainWindow::onLoginActionCliked);

    if (!UserManager::getInstance().isLogin()) {
        checkinManagementMenu = menuBar->addMenu(tr("归寝管理"));
        checkinManagementMenu->addAction(tr("归寝打卡"), this, &MainWindow::onCheckinActionCliked);
    }
    else {
        menuBar->clear();

        if (UserManager::getInstance().getRole() == UserManager::SystemAdmin) {
            userManagementMenu = menuBar->addMenu(tr("用户管理"));
            userManagementMenu->addAction(tr("用户管理"), this, &MainWindow::onUserManagementActionCliked);
        }
        else if (UserManager::getInstance().getRole() == UserManager::BusinessAdmin) {
            checkinManagementMenu = menuBar->addMenu(tr("归寝管理"));
            checkinManagementMenu->addAction(tr("宿舍归寝信息管理"), this, &MainWindow::onCheckinManagementActionCliked);
            jobManagementMenu = menuBar->addMenu(tr("职位管理"));
            jobManagementMenu->addAction(tr("宿舍人员职位管理"), this, &MainWindow::onJobManagementActionCliked);
            powerManagementMenu = menuBar->addMenu(tr("电量管理"));
            powerManagementMenu->addAction(tr("宿舍电量信息管理"), this, &MainWindow::onPowerManagementActionCliked);
            dormitoryManagementMenu = menuBar->addMenu(tr("宿舍管理"));
            dormitoryManagementMenu->addAction(tr("宿舍信息管理"), this, &MainWindow::onDormitoryManagementActionCliked);
        }
        else if (UserManager::getInstance().getRole() == UserManager::User) {
            checkinManagementMenu = menuBar->addMenu(tr("归寝管理"));
            checkinManagementMenu->addAction(tr("宿舍归寝信息管理"), this, &MainWindow::onCheckinManagementActionCliked);
//            powerManagementMenu = menuBar->addMenu(tr("电量管理"));
//            powerManagementMenu->addAction(tr("宿舍电量信息管理"), this, &MainWindow::onPowerManagementActionCliked);
//            dormitoryManagementMenu = menuBar->addMenu(tr("宿舍管理"));
//            dormitoryManagementMenu->addAction(tr("宿舍信息管理"), this, &MainWindow::onDormitoryManagementActionCliked);
        }
    }
}

template <class WidgetType>
QMdiSubWindow* MainWindow::createSubWindow(WidgetType* widget)
{
    QMdiSubWindow *subWindow = new QMdiSubWindow;
    subWindow->setWidget(widget);
    subWindow->setAttribute(Qt::WA_DeleteOnClose);

    subWindow->setWindowTitle(widget->windowTitle());

    QSize subWindowSize(widget->frameSize());
    subWindow->resize(subWindowSize);
    connect(widget, &WidgetType::requestClose, subWindow, &QMdiSubWindow::close); // Widget发出关闭请求时，关闭对应的QMdiSubWindow

    ui->mdiArea->addSubWindow(subWindow);
    return subWindow;
}

void MainWindow::onUserManagementActionCliked()
{
    createSubWindow(new UserManagementPage(this))->show();
}

void MainWindow::onCheckinManagementActionCliked()
{
    createSubWindow(new CheckinManagementPage(this))->show();
}

void MainWindow::onJobManagementActionCliked()
{
    createSubWindow(new JobManagementPage(this))->show();
}

void MainWindow::onPowerManagementActionCliked()
{
    createSubWindow(new PowerManagementPage(this))->show();
}

void MainWindow::onDormitoryManagementActionCliked()
{
    createSubWindow(new DormitoryManagementPage(this))->show();
}

void MainWindow::onCheckinActionCliked()
{
    createSubWindow(new CheckinPage(this))->show();
}

void MainWindow::onLoginActionCliked()
{
    LoginWindow* loginWindow = new LoginWindow();
    loginWindow->show();
}

